import { createRouter, createMemoryHistory, createWebHashHistory } from "vue-router";
import Login from "../views/login/Login.vue";
import Frame from "../views/main/Frame.vue";
import myabsent from "../views/absent/my.vue";
import subabsent from "../views/absent/sub.vue";
import publish from "../views/inform/publish.vue";
import list from "../views/inform/list.vue";
import details from "../views/inform/detail.vue";
import add from "../views/staff/add.vue";
import stafflist from "../views/staff/stafflist.vue";
import home from "../views/home/home.vue";

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: Login,
            meta:{needLogin:false}
        },
        {
            path: '/',
            name: 'frame',
            component: Frame,
            meta:{needLogin:true},
            children:[
                {path:'/myabsent',name:'myabsent',component:myabsent},
                {path:'/subabsent',name:'subabsent',component:subabsent},
                {path:'/inform/list',name:'informlist',component:list},
                {path:'/inform/publish',name:'informpublish',component:publish},
                {path:'/inform/details/:pk',name:'informdetails',component:details},
                {path:'/staff/add',name:'staff_add',component:add},
                {path:'/staff/stafflist',name:'stafflist',component:stafflist},
                {path:'/',name:'home',component:home}
            ]
          
        }
    ]
});



// 全局导航守卫
router.beforeEach((to, from, next) => {
    // 检查路由是否需要登录
    const isAuthenticated = !!localStorage.getItem('token'); // 检查 token 是否存在

    if (!isAuthenticated && to.meta.needLogin == true) {
        alert('还没有登录，请先登录');
        next({ name: 'login' }); // 使用 next() 来重定向到 login 页面
    } else {
        next(); // 继续导航
    }
});


export default router

